<?php

declare(strict_types=1);

namespace app\admin\controller\science;


use think\response\Json;
use think\response\View;
use app\admin\controller\Controller;

class Cate extends Controller
{
    /**
     * @var \app\admin\service\science\CateService
     */
    protected $service = null;
    /**
     * 重写中间件配置，科普文章列表API不需要认证
     */
    protected array $middleware = ['operLog'];
    public function initialize()
    {
        parent::initialize();
        $this->service = new \app\admin\service\science\CateService();
    }

    /**
     * 列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        return success($this->service->list());
    }

    public function lists(): Json
    {
        return success($this->service->lists());
    }

    public function detail(int $id): Json
    {
        return success($this->service->detail($id));
    }

      public function create(int $id = null): View
    {
        $data = [];
        if ($id) {
            $detail = $this->service->detail($id);
            if ($detail) {
                $data = $detail->toArray();
            }
        }
        $viewData = [
            'id' => $id
        ];
        return view('science/cate/create', $viewData);
    }
    public function update(int $id): Json
    {
        return success('更新成功', $this->service->update($id, request()));
    }

   
    public function add(): Json
    {
        return success('创建成功', $this->service->create(request()));
    }

    public function delete( ): Json
    {
        $id = $this->request->param('id', 0, 'intval');
        return $this->service->delete($id);
    }

    public function select(): Json
    {
        return success($this->service->select());
    }

    /**
     * 渲染创建/编辑页面
     * @return View
     */
    public function createPage(): View
    {
        return view('science/cate/create');
    }
}